import CryptoJS from 'crypto-js';
import Vue from 'vue';
import request from '@/router/axios';

export default {

	/**
	 *
	 * @param {*需要加密的字符串 注：对象转化为json字符串再加密} word
	 *
	 * @param {*aes加密需要的key值，这个key值后端同学会告诉你} keyStr
	 *
	 */

	encrypt(word, keyStr, iv) { // 加密

		var key = CryptoJS.enc.Utf8.parse(keyStr);

		iv = CryptoJS.enc.Utf8.parse(iv);


		var encrypted = CryptoJS.AES.encrypt(word, key, {
			iv: iv,
			mode: CryptoJS.mode.CBC,
			padding: CryptoJS.pad.ZeroPadding
		});

		return encrypted.toString();

	},

	decrypt(word, keyStr, iv) { // 解密

		var key = CryptoJS.enc.Utf8.parse(keyStr);

		iv = CryptoJS.enc.Utf8.parse(iv);

		var decrypt = CryptoJS.AES.decrypt(word, key, {
			iv: iv,
			mode: CryptoJS.mode.CBC,
			padding: CryptoJS.pad.ZeroPadding
		});

		return CryptoJS.enc.Utf8.stringify(decrypt).toString();

	},
	// 处理合作领域
	dealCooperateTypeInfo(cooperateType, cooperateTypeList) {
		let cooperateTypeInfo = []
		// 设置级联选项的默认选中值
		cooperateType.split(',').forEach(e => {
			if (e !== '') {
				cooperateTypeList.forEach(e1 => {
					let cooper = '';
					// 只有一级分类
					if (e === e1.cooperateMsgId) {
						cooperateTypeInfo.push(e1.cooperateName)
					} else {
						if (e1.children !== undefined) {
							if (e1.children.length > 0) {
								e1.children.forEach(e2 => {
									if (e === e2.cooperateMsgId) {
										cooper = e1.cooperateName + '/' + e2.cooperateName
										cooperateTypeInfo.push(cooper)
									}
								})
							}
						}
					}
				})
				return cooperateTypeInfo;
			}
		})
		return cooperateTypeInfo;
	},
	httpFn: (url, method, data) => {
		return new Promise((resolve, reject) => {
			request({
				url,
				method,
				data
			}).then(({data: res}) => {
				if(res.code && res.code == '000000') {
					resolve(res.resultBody);
				} else {
					reject(res);
					res.message && Vue.prototype.$message.error(res.message);
				}
			}).catch(e => {
				Vue.prototype.$message.error(e.message);
			});
		}, reject => reject)
	}

};
